package me.fanchaoo.web.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.fanchaoo.dbo.*;
import me.fanchaoo.enums.EnumLikeTargetType;
import me.fanchaoo.enums.EnumMomentType;
import me.fanchaoo.enums.EnumNotificationType;
import me.fanchaoo.enums.EnumStatus;
import me.fanchaoo.util.AssertUtils;
import me.fanchaoo.util.BeanCopyUtils;
import me.fanchaoo.util.PageUtils;
import me.fanchaoo.util.UserContext;
import me.fanchaoo.web.dao.mapper.*;
import me.fanchaoo.web.dto.LoginUserDTO;
import me.fanchaoo.web.dto.UserDTO;
import me.fanchaoo.web.request.CancelLikeRequest;
import me.fanchaoo.web.request.LikeRequest;
import me.fanchaoo.web.request.QueryLikeUserRequest;
import me.fanchaoo.web.request.SeeRequest;
import me.fanchaoo.web.response.BaseResponse;
import me.fanchaoo.web.service.LikeService;
import me.fanchaoo.web.service.NotificationService;
import me.fanchaoo.web.service.SeeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SeeServiceImpl implements SeeService {

    @Resource
    private SeeRelationMapper seeRelationMapper;
    @Resource
    private MomentMapper momentMapper;
    @Resource
    private ArtistQueryMapper artistQueryMapper;

    @Override
    @Transactional
    public void see(SeeRequest request) {
        // 从header获取
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest requestData = servletRequestAttributes.getRequest();
        String token = requestData.getHeader("token");
        Long artistId;
        Long userId;
        if (org.apache.commons.lang3.StringUtils.isBlank(token)) {
            ArtistDBO artistDBO = artistQueryMapper.getByRandomKey(request.getRandomKey());
            artistId =artistDBO.getId();
            userId= -1L;
        }else {
            LoginUserDTO loginUser = UserContext.getLoginUser();
            artistId = loginUser.getArtistId();
            userId = loginUser.getUserId();
        }


        EnumMomentType targetType = request.getTargetType();
        Long targetId = request.getTargetId();
        seeRelationMapper.insertSelective(new SeeRelationDBO(userId, targetType.getValue(), targetId, artistId));
        momentMapper.updateViewCount(artistId, targetId, 1);

    }



}
